package com.east.sng.dao;

import com.east.sng.entity.param.TeacherParam;
import com.east.sng.entity.result.TeacherFind;
import com.east.sng.entity.result.TeacherInfo;
import com.east.sng.utils.RedisCache;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author east
 * @date: 2020/12/30
 * @Description:
 * @Version: 1.0
 **/
@Repository
@CacheNamespace(implementation = RedisCache.class)
public interface TeacherDao {

    /**
     * 查询全部教师信息
     * @param params 教师参数列表
     * @return 教师实例对象列表
     */
    List<TeacherInfo> selectTeachers(Map<String, Object> params);

    /**
     * 根据教师id查询
     * @param teacherId 教师id
     * @return 教师实例对象
     */
    @Select("select t.*,ct.course_typename teacherTypeName,cr.classroom_name teacherClassRoomName "
            + "from teacher  t "
            + "left join course_type ct on t.teacher_type = ct.course_typeid "
            + "left join classroom cr on t.teacher_classroom = cr.classroom_id "
            + "where t.flag = 0 and t.teacher_id = #{teacherId} ")
    TeacherFind selectTeacher(Integer teacherId);

    /**
     * 根据课程类型查询授课教师
     * @param courseTypeId 课程类型id
     * @return 教师id和名称列表
     */
    @Select("select  t.teacher_id teacherId,t.teacher_name teacherName "
            + "from  teacher t "
            + "left join course_type  ct on  ct.course_typeid = t.teacher_type "
            + "where t.flag = 0 and ct.course_typeid=#{courseTypeId} ")
    List<Map<String, Object>> selectTeachersColumns(Integer courseTypeId);

    /**
     * 添加教师
     * @param teacherParam 教师实例对象
     */
    @Insert("insert  into  teacher "
            + "values(null,#{teacher.teacherName},#{teacher.teacherAge},#{teacher.teacherSex},#{teacher.teacherPhone},#{teacher.teacherType},#{teacher.teacherClassRoom}) ")
    void insertTeacher(@Param("teacher") TeacherParam teacherParam);

    /**
     * 修改教师
     * @param teacherParam 教师实例对象
     */
    @Update("update teacher set teacher_name=#{teacher.teacherName},teacher_age=#{teacher.teacherAge},teacher_sex=#{teacher.teacherSex} "
            + ",teacher_phone=#{teacher.teacherPhone},teacher_type=#{teacher.teacherType},teacher_classroom=#{teacher.teacherClassRoom}  "
            + "where teacher_id = #{teacher.teacherId} ")
    void updateTeacher(@Param("teacher") TeacherParam teacherParam);

    /**
     * 删除或恢复教师
     * @param teacherId 教师id
     * @param flag 删除标志
     * @return 受影响行数
     */
    @Delete("update teacher set flag = #{flag} where teacher_id = #{teacherId} ")
    Integer deleteOrRecoverTeacher(@Param("teacherId") Integer teacherId,@Param("flag") Integer flag);

    /**
     * 批量或恢复删除教师
     * @param teacherIds 教师id列表
     * @param flag 删除标志
     * @return 受影响行数
     */
    @Delete({"<script>","update teacher set flag = #{flag} where teacher_id in",
            "<foreach collection='list' item='TeacherId' open='(' separator=',' close=')'>",
            "#{TeacherId}",
            "</foreach>",
            "</script>"})
    Integer batchDeleteOrRecoverTeachers(@Param("list") List<Integer> teacherIds,@Param("flag") Integer flag);

}
